//
//  RBGroupDoneVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/11.
//

#import "RBGroupDoneVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "UIScrollView+RBAddHorizontal.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBTimer.h"
#import "RBGroupJoinResponse.h"
#import "UIViewController+RBAdd.h"
#import "UIImageView+RBAdd.h"
#import "RBGoodsDetailWebView.h"
#import "RBGroupAloneBuyVC.h"

@interface RBGroupDoneVC ()

@property(nonatomic, strong)UIScrollView *scrollView;

@property(nonatomic, strong)IBOutlet UIView *goodsView;
@property(nonatomic, strong)IBOutlet UILabel *shopL;
@property(nonatomic, strong)IBOutlet UIImageView *goodsImgView;
@property(nonatomic, strong)IBOutlet UILabel *goodsNameL;
@property(nonatomic, strong)IBOutlet UILabel *groupNumL;
@property(nonatomic, strong)IBOutlet UILabel *priceL;

@property(nonatomic, strong)UILabel *dayL;
@property(nonatomic, strong)UILabel *hourL;
@property(nonatomic, strong)UILabel *minuteL;
@property(nonatomic, strong)UILabel *secondL;

@property(nonatomic, strong)RBTimer *timer;
/**
    尝试通过刷新数据来校正页面
    后台数据不正确的情况下会校正失败,为了防止无休止刷新,只做一次校正
 */
@property(nonatomic, assign) BOOL triedToAjustByReloadData;

@property(nonatomic, strong) RBGroupJoinResponse *response;

@end

@implementation RBGroupDoneVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    __weak typeof(self) weak_self = self;
    [self.timer startTimerWithInterval:1 block:^(float countF, NSInteger countI) {
        [weak_self reloadTimeDisplay];
    }];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = self.type == 0? @"开团成功": @"拼团详情";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    [self.goodsView zoomSubViewsWithSelf];
    self.goodsView.height = Zoom(self.goodsView.height);
}

- (void)reloadTimeDisplay
{
    if(!self.response) return;
    if(self.response.data.status != 1) return;
    if(!self.triedToAjustByReloadData)
    {
        double seconds = self.response.data.end_time - [NSDate.date timeIntervalSince1970];
        if(seconds <= 0)
        {//倒计时结束,刷新一次状态
            self.triedToAjustByReloadData = YES;
            [self getData];
            return;
        }
    }
    
    double seconds = self.response.data.end_time - [NSDate.date timeIntervalSince1970];
    seconds = MAX(seconds, 0);
    NSArray *components = [RanTool dayHourMinuteSecondFromInterval:seconds];
    self.dayL.text = [NSString stringWithFormat:@"%@", components[0]];
    self.hourL.text = [NSString stringWithFormat:@"%@", components[1]];
    self.minuteL.text = [NSString stringWithFormat:@"%@", components[2]];
    self.secondL.text = [NSString stringWithFormat:@"%@", components[3]];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    CGFloat goodsTop = 0;
    if(self.type == 0)
    {
        goodsTop = Zoom(38);
        [self.scrollView addView:[RanTool imgViewWithMode:0 radius:0 imgName:@"groupDone"] withHeight:Zoom(91) width:Zoom(91) top:Zoom(48) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        [self.scrollView addView:[RanTool labelWithFontSize:Zoom(20) weight:2 color:[RBThemeConfig textBlackColor] align:1 text:@"恭喜您开团成功"] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(34) bottom:0 left:0 right:0 centerX:0];
    }
    else
    {
        goodsTop = Zoom(10);
        [self.scrollView addView:[self groupOwnerView] withHeight:Zoom(96) width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    }
    [self.scrollView addView:self.goodsView withHeight:self.goodsView.height width:CGFLOAT_MIN top:goodsTop bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    self.shopL.text = self.response.data.store_name;
    [self.goodsImgView setImageWithUrl:self.response.data.goods_picurl placeHolder:nil];
    self.goodsNameL.text = self.response.data.goods_good_name;
    self.groupNumL.text = [NSString stringWithFormat:@"%zd人团", self.response.data.group_people_max];
    self.priceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:self.response.data.group_price];
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textGrayColor] string:@"　"];
        [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[RBThemeConfig textGrayColor] string:[NSString stringWithFormat:@"¥%@", self.response.data.goods_sell_price] lineSpacing:0 througnLineHeight:1 throughLineColor:[RBThemeConfig textGrayColor]];
    }];
    [self.scrollView addView:self.groupView withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[RanTool labelWithFontSize:Zoom(18) weight:2 color:[RBThemeConfig textBlackColor] align:1 text:@"拼团规则"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(39) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        
        RBGoodsDetailWebView *webView = RBGoodsDetailWebView.new;
        webView.autoUpdateHeight = YES;
        [view view_addView:webView withHeight:10 width:CGFLOAT_MIN top:Zoom(26) bottom:Zoom(45) left:Zoom(13) right:Zoom(13) centerX:0];
        webView.content = self.response.data.guize;
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(40) left:Zoom(13) right:Zoom(13) centerX:0];
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"group_activity_id"] = self.group_activity_id;
        dicM[@"order_id"] = self.orderId;
    } path: self.type == 2? @"my_group_goods_show": @"hu_group_ongoing" post:YES log:YES responseClass:RBGroupJoinResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:nil reloadBlock:^{
        [weak_self getData];
    }];
}

#pragma mark - Event

- (void)shareBtnTapped
{
    
}

- (void)joinBtnTapped
{
    RBGroupAloneBuyVC *vc = RBGroupAloneBuyVC.new;
    vc.type = 2;
    vc.group_id = self.response.data.group_id;
    vc.group_activity_id = self.group_activity_id;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (RBTimer *)timer
{
    if (!_timer)
    {
        self.timer = RBTimer.new;
    }
    return _timer;
}

- (UIView *)groupView
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    [view rbSetCornerRadius:Zoom(8)];
    if(self.response.data.status == 2)
    {
        [view view_addView:[RanTool labelWithFontSize:Zoom(18) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@"恭喜您已成团"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(50) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        [view view_addView:[RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"我们会为您尽快发货"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(17) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        [view view_addView:[RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"个人中心—我的订单查看详情"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    }
    else if(self.response.data.status == 1)
    {
        [view view_addView:[RanTool labelWithFontSize:Zoom(18) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:[NSString stringWithFormat:@"等待成团 仅剩%zd个名额", self.response.data.surplus_num]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(50) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    }
    else
    {
        [view view_addView:[RanTool labelWithFontSize:Zoom(18) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:@"很遗憾拼团失败"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(50) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        [view view_addView:[RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"规定时间内未达到人数"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(17) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        [view view_addView:[RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"系统将在1-2个工作日自动退换款项"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    }
    
    [view view_addView:({
        UIScrollView *view = UIScrollView.new;
        view.showsHorizontalScrollIndicator = NO;
        [view h_rbSetContentHeight:Zoom(127)];
        NSInteger maxPeople = self.response.data.group_people_max;
        NSInteger count = self.response.data.member_list.count;
        
        NSInteger value = count;
        if (count == maxPeople) {
            value = value - 1;
        }
        for (NSInteger i = 0; i <= value; i++) {
            [view h_addView:[self groupMemberWithData:i < count ? self.response.data.member_list[i]:nil] withHeight:CGFLOAT_MIN width:Zoom(63) top:0 bottom:0 left:i? Zoom(48): Zoom(33) right:Zoom(33) centerY:NO];
        }
        view;
    }) withHeight:Zoom(127) width:CGFLOAT_MIN top:Zoom(39) bottom:Zoom(48) left:0 right:0 centerX:0];
    
    if(self.response.data.status == 1)
    {
        [view view_addView:({
            UIScrollView *view = UIScrollView.new;
            view.showsHorizontalScrollIndicator = NO;
            [view h_rbSetContentHeight:Zoom(23)];
            
            [view h_addView:[RanTool labelWithFontSize:Zoom(18) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"距离结束"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(30) right:0 centerY:YES];
            //间隙
            [view h_addView:UIView.new withHeight:1 width:Zoom(20) top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_addView:({
                UILabel *view = [self groupTimeL];
                self.dayL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:Zoom(22) top:0 bottom:0 left:Zoom(6) right:0 centerY:0];
            [view h_addView:[self groupTimeUnitLWithUnit:@"天"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(6) right:0 centerY:NO];
            [view h_addView:({
                UILabel *view = [self groupTimeL];
                self.hourL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:Zoom(22) top:0 bottom:0 left:Zoom(6) right:0 centerY:0];
            [view h_addView:[self groupTimeUnitLWithUnit:@"时"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(6) right:0 centerY:NO];
            [view h_addView:({
                UILabel *view = [self groupTimeL];
                self.minuteL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:Zoom(22) top:0 bottom:0 left:Zoom(6) right:0 centerY:0];
            [view h_addView:[self groupTimeUnitLWithUnit:@"分"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(6) right:0 centerY:NO];
            [view h_addView:({
                UILabel *view = [self groupTimeL];
                self.secondL = view;
                view;
            }) withHeight:CGFLOAT_MIN width:Zoom(22) top:0 bottom:0 left:Zoom(6) right:0 centerY:0];
            [view h_addView:[self groupTimeUnitLWithUnit:@"秒"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(6) right:0 centerY:NO];
            
            view;
        }) withHeight:Zoom(23) width:CGFLOAT_MIN top:Zoom(34) bottom:Zoom(48) left:0 right:0 centerX:0];
    }
    [view view_addView:UIView.new withHeight:Zoom(27) width:Zoom(304) top:0 bottom:Zoom(48) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    if(self.response.data.status == 1)
    {
        if([self.response.data.is_can isEqualToString:@"N"])
        {
            [view view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:1 text:@"立即参团"];
                view.backgroundColor = [RBThemeConfig buttonBlueColor];
                [view rbSetCornerRadius:Zoom(8)];
                [view rbAddTapGesWithTarget:self action:@selector(joinBtnTapped)];
                view;
            }) withHeight:Zoom(49) width:Zoom(304) top:Zoom(12) bottom:Zoom(48) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
        }
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:1 text:@"分享"];
            view.backgroundColor = [RBThemeConfig buttonBlueColor];
            [view rbSetCornerRadius:Zoom(8)];
            [view rbAddTapGesWithTarget:self action:@selector(shareBtnTapped)];
            view;
        }) withHeight:Zoom(49) width:Zoom(304) top:Zoom(12) bottom:Zoom(48) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    }
    
    return view;
}

- (UIView *)groupMemberWithData: (RBGroupJoinMemberModel *)model
{
    UIView *view = UIView.new;
    [view view_addView:[RanTool imgViewWithMode:2 radius:Zoom(63/2.0) imgName: model? model.member_avatar: @"groupJoin"] withHeight:Zoom(63) width:Zoom(63) top:0 bottom:CGFLOAT_MIN left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    [view view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:1 text:model? model.member_nickname: @"　"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    if([model.is_duizhang isEqualToString:@"Y"])
    {
        UILabel *roleL = [RanTool labelWithFontSize:Zoom(14) weight:1 color:UIColor.whiteColor align:1 text:@"团长"];
        roleL.backgroundColor = [UIColor colorWithHexString:@"#FF9242"];
        [roleL rbSetCornerRadius:Zoom(9)];
        [view addSubview:roleL];
        [roleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(Zoom(40));
            make.height.mas_equalTo(Zoom(21));
            make.bottom.centerX.mas_equalTo(0);
        }];
    }
    
    return view;
}

- (UILabel *)groupTimeL
{
    UILabel *view = [RanTool labelWithFontSize:Zoom(14) weight:0 color:UIColor.whiteColor align:1 text:@"00"];
    view.backgroundColor = [RBThemeConfig buttonBlueColor];
    [view rbSetCornerRadius:Zoom(3)];
    return view;
}

- (UILabel *)groupTimeUnitLWithUnit: (NSString *)unit
{
    return [RanTool labelWithFontSize:Zoom(14) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:unit];
}

- (UIView *)groupOwnerView
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    [view rbSetCornerRadius:Zoom(8)];
    
    UIImageView *imgView = [RanTool imgViewWithMode:2 radius:Zoom(31) imgName:self.response.data.duizhang_avatar];
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(16));
        make.top.mas_equalTo(Zoom(14));
        make.width.height.mas_equalTo(Zoom(62));
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(16) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:self.response.data.duizhang_nickname];
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(15));
        make.top.mas_equalTo(Zoom(22));
        make.right.mas_equalTo(-Zoom(16));
    }];
    
    UILabel *desL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[UIColor colorWithHexString:@"#E75341"] align:1 text:[NSString stringWithFormat:@" 发起了%zd人团 ", self.response.data.group_people_max]];
    desL.backgroundColor = [UIColor colorWithHexString:@"#FFFAF8"];
    [desL rbSetCornerRadius:Zoom(11)];
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameL);
        make.top.mas_equalTo(nameL.mas_bottom).offset(Zoom(9));
        make.height.mas_equalTo(Zoom(21));
    }];
    
    return view;
}

@end
